﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CH.Reia.Entity;
using System.Configuration;

namespace CH.Reia.Mvc.Controllers
{
    public class LinkController : Controller
    {
        public ActionResult Index()
        {
            var reiaDomain = Reia.Domain.Domain.CreateDomain();
            
            var linkList = new Link
            {
                Domain = ConfigurationManager.AppSettings["RepositoryDomain"]
            };

            return View(reiaDomain.LinkList(linkList));
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Link model)
        {
            ModelState.Clear();

            if (string.IsNullOrEmpty(model.Url))
                ModelState.AddModelError("Url", "Url é necessário!");

            if (string.IsNullOrEmpty(model.Title))
                ModelState.AddModelError("Title", "Title é necessário!");

            if (string.IsNullOrEmpty(model.Note))
                ModelState.AddModelError("Note", "Note é necessário!");

            if (ModelState.IsValid)
            {
                var reiaDomain = Reia.Domain.Domain.CreateDomain();
                var linkSave = new Link
                {
                    LinkId = Guid.NewGuid().ToString(),
                    Url = model.Url,
                    Title = model.Title,
                    Note = model.Note,
                    CreateAt = DateTime.Now,
                    IsPrivate = model.IsPrivate,
                    Domain = ConfigurationManager.AppSettings["RepositoryDomain"]
                };

                if (reiaDomain.LinkSave(linkSave).Count > 0)
                    return RedirectToAction("Index", "Link");
            }
            return View(model);
        }

        public ActionResult Edit(string id)
        {
            var reiaDomain = Reia.Domain.Domain.CreateDomain();
            var linkId = new Guid(id).ToString();
            
            var linkLoad = new Link
            {
                LinkId = linkId,
                Domain = ConfigurationManager.AppSettings["RepositoryDomain"]
            };

            return View(reiaDomain.LinkLoad(linkLoad).FirstOrDefault());
        }

        [HttpPost]
        public ActionResult Edit(Link model)
        {
            ModelState.Clear();

            if (string.IsNullOrEmpty(model.Url))
                ModelState.AddModelError("Url", "Url é necessário!");

            if (string.IsNullOrEmpty(model.Title))
                ModelState.AddModelError("Title", "Title é necessário!");

            if (string.IsNullOrEmpty(model.Note))
                ModelState.AddModelError("Note", "Note é necessário!");

            if (ModelState.IsValid)
            {
                var reiaDomain = Reia.Domain.Domain.CreateDomain();
                var linkSave = new Link
                {
                    LinkId = model.LinkId,
                    Url = model.Url,
                    Title = model.Title,
                    Note = model.Note,
                    CreateAt = DateTime.Now,
                    IsPrivate = model.IsPrivate,
                    Domain = ConfigurationManager.AppSettings["RepositoryDomain"]
                };

                if (reiaDomain.LinkUpdate(linkSave).Count > 0)
                    return RedirectToAction("Index", "Link");
            }
            return View(model);
        }

        public ActionResult Delete(string id)
        {
            var reiaDomain = Reia.Domain.Domain.CreateDomain();
            var linkId = new Guid(id).ToString();

            var linkDelete = new Link
            {
                LinkId = linkId,
                Domain = ConfigurationManager.AppSettings["RepositoryDomain"]
            };

            reiaDomain.LinkDelete(linkDelete);

            return RedirectToAction("Index", "Link");
        }
    }
}
